using System;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://123.30.51.19:1102/MTReceiver.asmx")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class MTReceiver : System.Web.Services.WebService
{
    public AuthHeader Authentication;

    public class AuthHeader : SoapHeader
    {
        public string Username;
        public string Password;
    }

    [SoapHeader("Authentication")]
    [WebMethod]
    public int SendMsgOut(string mobile_no, string message_in, string message_out, string service_id, string command_code, string message_type, string request_id, string total_message, string message_index, string is_more, string content_type)
    {
        if (Authenticated())
        {
            return InsertMsgOut(mobile_no, message_out, service_id, command_code, message_type, request_id, total_message, message_index, is_more, content_type);
        }
        else
            return -4;
    }

    private bool Authenticated()
    {
        if (Authentication.Username == "r3c3ivM90ut" && Authentication.Password == "r3c3i@@M5g0ut") return true;
        else
            return false;
    }

    private int InsertMsgOut(string mobile_no, string message_out, string service_id, string command_code, string message_type, string request_id, string total_message, string message_index, string is_more, string content_type)
    {
        if (message_out.Trim().Replace(" ", "") != "")
        {
            string connStr = ConfigurationManager.ConnectionStrings["SMSSERVICE"].ToString();
            int ret = -1;

            if (service_id.Length > 3)
                service_id = service_id.Substring(service_id.Length - 4);

            if (Array.IndexOf<string>(new string[] { "8078", "8178", "8278", "8378", "8478", "8578", "8678", "8778" }, service_id) < 0)
                return 4;

            try
            {
                string storeProc = "vn1102_Insert_AllMsgOut_Partner";

                SqlConnection cnn = new SqlConnection(connStr);
                SqlCommand cmd = new SqlCommand(storeProc, cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                
                cmd.Parameters.Add("@MobileNo", SqlDbType.VarChar, 20).Value = mobile_no;
                cmd.Parameters.Add("@MsgOutContent", SqlDbType.VarChar, 200).Value = message_out;
                cmd.Parameters.Add("@ServiceID", SqlDbType.VarChar, 50).Value = service_id;
                cmd.Parameters.Add("@CommandCode", SqlDbType.VarChar, 50).Value = command_code;

                try
                {
                    cmd.Parameters.Add("@MsgType", SqlDbType.TinyInt).Value = byte.Parse(message_type);
                }
                catch { return 5; }

                try
                {
                    cmd.Parameters.Add("@RequestID", SqlDbType.Int).Value = int.Parse(request_id);
                }
                catch { return 6; }

                try
                {
                    cmd.Parameters.Add("@MsgTotal", SqlDbType.TinyInt).Value = byte.Parse(total_message);
                }
                catch { return 7; }

                try
                {
                    cmd.Parameters.Add("@MsgIndex", SqlDbType.TinyInt).Value = byte.Parse(message_index);
                }
                catch { return 8; }

                try
                {
                    cmd.Parameters.Add("@IsMore", SqlDbType.TinyInt).Value = byte.Parse(is_more);
                }
                catch { return 9; }

                try
                {
                    cmd.Parameters.Add("@ContentType", SqlDbType.TinyInt).Value = byte.Parse(content_type);
                }
                catch { return 10; }

                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();

                ret = 0;
            }
            catch { return -1; }

            return ret;
        }
        else
            return 3;
    }
}
